from django.contrib.auth.models import User
from django.db import models
from utils.modelsMixin import ModelsLsMixin, DateTimeModelMixin


# Create your models here.

# 商品分类表
#1  python基础  null
#2      变量    1
#3      判断    1
#4  数据库      null
#5      mysql  4
class Classification(ModelsLsMixin):
    name = models.CharField(max_length=100,unique=True,verbose_name="分类名")
    # 上一级 父级
    parent = models.ForeignKey("self",on_delete=models.SET_NULL,null=True,blank=True)

    class Meta:
        db_table = 'classification'

    def __str__(self):
        return f'{self.id},{self.name}'

# 商品模型
class Commodity(ModelsLsMixin):
    STATUS_CHOICES = ((0, "未发布"), (1, "发布"))
    name = models.CharField(max_length=100,verbose_name="商品名",unique=True)
    caption = models.CharField(max_length=100,verbose_name="副标题")
    brand = models.CharField(max_length=100,verbose_name="品牌",null=True,blank=True)
    price = models.DecimalField(max_digits=12,decimal_places=2,verbose_name="价格")
    stock = models.IntegerField(verbose_name="库存")
    pack = models.TextField(verbose_name="包装信息",null=True,blank=True)
    sales = models.IntegerField(verbose_name="销量",default=0)
    comments = models.IntegerField(default=0, verbose_name="评价数")
    status = models.IntegerField(default=0, verbose_name="状态", choices=STATUS_CHOICES)
    detail = models.TextField(verbose_name="详情",blank=True,null=True)
    serviceafter_sale = models.TextField(verbose_name="售后服务")
    # classification_set ==>classification1
    classification1 = models.ForeignKey(Classification,on_delete=models.SET_NULL,null=True,blank=True,related_name="classification1")
    # classification_set
    classification2 = models.ForeignKey(Classification,on_delete=models.SET_NULL,null=True,blank=True,related_name="classification2")
    class Meta:
        db_table = 'commodity'
        ordering = ["-sales","-comments","-created_time"]


# 商品图
class CommodityImg(ModelsLsMixin):
    src = models.CharField(max_length=100,null=True,blank=True)
    priority = models.IntegerField(default=0, verbose_name="优先级")
    commodity = models.ForeignKey(Commodity,on_delete=models.CASCADE,null=True,blank=True)

    class Meta:
        db_table = 'commodityimg'
        ordering = ["-priority"]





# 购物车模型
class ShoppingCart(DateTimeModelMixin):
    number = models.IntegerField(default=1, verbose_name='数量')
    commodity = models.ForeignKey(Commodity, on_delete=models.CASCADE, verbose_name='商品')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')

    class Meta:
        ordering = ['-update_time']
        db_table = 'shoppingcart'

# 购物车  商品数量   commodity（商品id）  用户信息

# 订单 订单号 商品总数  订单金额  支付方式  收货地址  用户信息


# 订单详情表  订单号  commodity（商品id） 单个商品数量  单价


# 订单模型
class Order(DateTimeModelMixin):
    PAY_METHOD_CHOICES = (
        (1, '支付宝'),
        (2, '货到付款')
    )
    STATUS_CHOICES = (
        (1, '待支付'),
        (2, '待发货'),
        (3, '待收货'),
        (4, '待评价'),
        (5, '已完成'),
        (6, '已取消'),
        (7, '申请退款'),
        (8, '退款成功')
    )
    order_id = models.CharField(max_length=64, primary_key=True, verbose_name='订单号')
    #购买了多少商品
    total_count = models.IntegerField(default=1, verbose_name='商品总数')
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='商品总金额')
    pay_method = models.IntegerField(verbose_name='支付方式', default=1, choices=PAY_METHOD_CHOICES)
    status = models.IntegerField(verbose_name='订单状态', default=1, choices=STATUS_CHOICES)
    #收货地址不绑定外键，防止下单之后，修改收货地址，订单地址也被改变
    address = models.TextField(verbose_name='收货地址')

    user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name='用户')

    class Meta:
        ordering = ['-created_time']
        db_table = 'order'

# 订单详情
class OrderGoods(DateTimeModelMixin):
    SCORE_CHOICES = (
        (0, '零星'),
        (1, '一星'),
        (2, '两星'),
        (3, '三星'),
        (4, '四星'),
        (5, '五星'),
    )
    number = models.IntegerField(default=1, verbose_name='商品数')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='单价')
    comment = models.TextField(verbose_name='评价', null=True, blank=True)
    score = models.IntegerField(null=True, blank=True, verbose_name='评分', choices=SCORE_CHOICES)

    is_anonymous = models.BooleanField(default=False, verbose_name='匿名评价')

    # 那个订单的详情数据  一个订单多个商品
    order = models.ForeignKey(Order, on_delete=models.CASCADE, verbose_name='订单')
    # 商品  一个商品可以有多个订单  一件商品可以被多人购买 生成多个订单
    commodity = models.ForeignKey(Commodity, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='商品')

    class Meta:
        db_table = 'ordergoods'




# 支付宝订单模型
class Payment(models.Model):
    create_time = models.DateTimeField(auto_now_add=True,verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="修改时间")
    trade_id = models.CharField(max_length=100,unique=True,verbose_name="支付宝订单号")
    order = models.ForeignKey(Order,on_delete=models.CASCADE,verbose_name="订单")
    class Meta:
        ordering = ['-create_time']
        db_table = 'payment'







